package com.sunshuo.springsecuritydemo.service;

import com.sunshuo.springsecuritydemo.dao.UserRoleDao;
import com.sunshuo.springsecuritydemo.pojo.UserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName UserRoleService
 * @Description
 * @Author sunshuo
 * @Date 2021/11/7 21:03
 */
@Service
public class UserRoleService {

    private UserRoleDao userRoleDao;

    @Autowired
    public void setUserRoleDao(UserRoleDao userRoleDao) {
        this.userRoleDao = userRoleDao;
    }

    public List<UserRole> getRoleRelationsByUserId(String id) {
        return userRoleDao.getRoleRelationsByUserId(id);
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void updateUserRole(String userId, List<UserRole> userRoles) throws Exception {
        try {
            userRoleDao.deleteByUserId(userId);
            for (UserRole userRole: userRoles) {
                userRoleDao.add(userRole);
            }
        } catch (Exception e) {
            throw new Exception(e);
        }
    }
}
